using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class GUIScoreBoard : MonoBehaviour {
	public float textureTime = 3;
	
	public Texture2D[] hatTextures = new Texture2D[]{};
	public Texture2D killTexture;
	public bool useBackground = true;
	public bool debug;
	public Vector2 scoreBoardPosition;
	public Vector2 scoreBoardSize;
	public bool allignToRightSide = true;
	public Texture2D scoreBackground;
	private List<Vector2> scores = new List<Vector2>();
	private List<float> timers = new List<float>();
	public int maxScores = 5;
	
	private int killer;
	private int killed;
	
	// Use this for initialization
	void Start () {
		if (scoreBackground == null && useBackground){
			scoreBackground = new Texture2D(5,5);
			scoreBackground.Apply();
		}
	}

	// Update is called once per frame
	void Update () {
		for (int i = 0; i < scores.Count;i++){
			if (Time.time > timers[i]){
				scores.RemoveAt(i);
				timers.RemoveAt(i);
			}
		}
	}
	
	void OnGUI(){
		
		/*-------------------------------------------
		 * Scoreboard
		 * 
		 -----------------------------------------*/
		float position;
		if (allignToRightSide){
			position = Screen.width-scoreBoardPosition.x;
		}
		else
			position = scoreBoardPosition.x;
		
		GUI.BeginGroup(new Rect(position, scoreBoardPosition.y,scoreBoardSize.x,scoreBoardSize.y));
			if (useBackground)
				GUI.DrawTexture(new Rect(0,0,scoreBoardSize.x,scoreBoardSize.y),scoreBackground,ScaleMode.StretchToFill);
			for (int i = 0; i < Mathf.Clamp(scores.Count,0,maxScores); i++){
					GUI.DrawTexture(new Rect(0,scoreBoardSize.y/maxScores*i,scoreBoardSize.x/3,scoreBoardSize.y/maxScores),hatTextures[(int)scores[i].x],ScaleMode.ScaleToFit);
					GUI.DrawTexture(new Rect(scoreBoardSize.x/3,scoreBoardSize.y/maxScores*i,scoreBoardSize.x/3,scoreBoardSize.y/maxScores),killTexture,ScaleMode.ScaleToFit);
					GUI.DrawTexture(new Rect(scoreBoardSize.x/3*2,scoreBoardSize.y/maxScores*i,scoreBoardSize.x/3,scoreBoardSize.y/maxScores),hatTextures[(int)scores[i].y],ScaleMode.ScaleToFit);
					//Debug.Log(scores[i].x + " killed " + scores[i].y);
			}
		GUI.EndGroup();
		if (debug){
			GUI.BeginGroup(new Rect(Screen.width/2-200,Screen.height-400,400,400),GUIStyle.none);
				string killerString;
				string killedString;
				killerString = GUI.TextField(new Rect(0,0,100,25),killer.ToString(),"box");
				GUI.Label(new Rect(0,30,100,25),"killed","box");
				killedString = GUI.TextField(new Rect(0,60,100,25),killed.ToString(),"box");
				killer = int.Parse(killerString);
				killed = int.Parse(killedString);
				if (GUI.Button(new Rect(0,90,100,25),"KILL","box")){
					addKill(killer,killed);
				}
			GUI.EndGroup();	
		}
	}
				
	public void addKill(int killer,int killed){
			if (scores.Count > maxScores){
				scores.RemoveAt(0);
				timers.RemoveAt(0);
			}
			scores.Add(new Vector2(killer,killed));
			timers.Add(Time.time + textureTime);
		
	}
				
}
